package cn.hejiang.org.service.impl;

import cn.hejiang.basic.exception.BusinessException;
import cn.hejiang.basic.service.impl.BaseServiceImpl;
import cn.hejiang.org.domain.Employee;
import cn.hejiang.org.domain.Shop;
import cn.hejiang.org.mapper.EmployeeMapper;
import cn.hejiang.org.mapper.ShopMapper;
import cn.hejiang.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

// 继承基础CRUD，只写扩展功能
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 校验两次密码是否一致
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码都不一致，滚去重输");
        }

        // 拿t_employee检查username phone email是否已存在，如果存在则返回提示信息
        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if (employee != null) {
            throw new BusinessException("亲，此账号已被注册，如果忘记密码请点击找回密码");
        }

        // 账号为被注册则保存信息
        // 保存员工信息，返回自增长id
        employeeMapper.save(admin);
        // 保存店铺信息
        shop.setAdmin(admin);
        // 返回id
        shopMapper.save(shop);

        // 修改员工所属的店铺id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);

    }
}
